﻿using DGZImp.Core.Extensions;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DGZImp.MdsProject.Models
{
    public static class MdsExtends
    {
        /// <summary>
        /// 一个类实例的扩展方法，能够通过反射获取属性列表，返回一个包含Name，Description，Value的List，并且按照属性的描述属性进行排序
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<PropsItem> ToPropValueList(this object obj)
        {
            var res = new List<PropsItem>();
            if (obj != null)
            {
                var properties = obj.GetType().GetProperties();
                foreach (var item in properties)
                {
                    var i = new PropsItem
                    {
                        Name = item.Name,
                        Description = item.GetCustomAttribute<DescriptionAttribute>()?.Description ?? "",
                        Value = item.PropertyType.IsEnum ? ((Enum)item.GetValue(obj)).GetDescription() : item.GetValue(obj),
                    };
                    if (i.Description.IsValuable())
                        res.Add(i);
                }
            }
            return res.OrderBy(x => x.Description).ToList();
        }
    }
}
